<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <!-- 布局组件  左侧结构-->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">哈尔滨鼎盛科技股份有限公司</span>
              </el-col>
              <!-- 右侧结构 -->
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDept('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 使用树形控件来渲染结构 -->
            <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
              <!-- 使用插槽自定义结构 -->
              <!-- 插槽本身提供了两个数据 data:当前行数据 -->
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDept(data.id)"
                            >添加部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDept(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDept(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 编辑/新增组织架构弹框 -->
        <el-dialog
          :title="isEdit ? '编辑组织架构' : '新增组织架构子部门'"
          width="50%"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 添加子组件弹层 -->
          <!-- 数据回显 显示数据覆盖原因: 子组件没有销毁 所以每次都不会 created -->
          <!-- 我们只需要在弹窗关闭的时候 将子组件 销毁即可 子组件被重新创建 即可触发 created -->
          <deptDialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-list="updateList"
            @close="closeDialog"
          />
        </el-dialog>
      </el-card>
      <div>
        <!-- <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" /> -->

      </div>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
// 引入子组件
import deptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      showDialog: false, // 控制添加或编辑弹框展示
      activeName: 'first', // 被激活的 Tab 标签页
      treeData: [
        {
          name: '财务部',
          manager: '总裁办',
          children: [
            {
              name: '财务 1 部',
              manager: '老王'
            },
            {
              name: '财务 2 部',
              manager: '老李'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children', // 指定描述父子关系的字段
        label: 'name'
      },
      parentId: '',
      isEdit: false // 是否是编辑状态
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    handleNodeClick(data, node, curNode) {
      console.log(data)
      console.log(node)
      console.log(curNode)
    },
    // 获取组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      // 组织架构数据 赋值
      if (!res.success) return this.$message.error(res.message)
      this.treeData = tranListToTreeData(res.data.depts)
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        code: item.code,
        pid: item.pid
      }))
    },
    addDept(id) {
      // console.log(123)
      this.parentId = id
      this.showDialog = true
      // 点击新增时候 将 isEdit 设置为 false
      this.isEdit = false
    },
    updateList() {
      this.showDialog = false
      this.getDepartments()
    },
    // 删除部门
    async delDept(id) {
      // 绘制询问框
      // console.log(123)
      const confirmRes = await this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击取消给任务取消提示
      // 如果返回的结果是 cancel 说明用户取消了删除
      if (confirmRes === 'cancel') return this.$message('您取消了删除')
      // 点击确定 调用api接口 渲染 更新
      // 调用删除接口
      const res = await delDepartment(id)
      // 删除成功需要给用户进行提示

      this.$message.success(res.message)
      // 删除后需要重新获取当前页面数据
      this.getDepartments()
    },
    // 子组件弹窗点击取消按钮 隐藏弹窗
    closeDialog() {
      this.showDialog = false
    },
    // 编辑
    editDept(id) {
      this.parentId = id
      this.showDialog = true
      // 点击编辑时 isEdit 设置为true
      this.isEdit = true
    }

  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
